<?php

/**
 * --------------------------------
 * Default UserController
 *
 * 用户登录管理的统一类  UserController
 * 
 * --------------------------------
 */

class UserController extends BaseController {

	/**
	 * Session判断的统一接口
	 */
	public static function check_session(){
		$session_user = Session::get('nativeid');
		$session_password = Session::get('password');
		if($session_user ==''||$session_password ==''){
			return Response::json(['error'=>true,'message'=>'缺少重要参数'],201);
		}
		$user_result = DB::select("select id,nativeid,name,password,role_id from teacher where nativeid = '".$session_user."'");
		if(!$user_result){
			return Response::json(['error'=>true,'message'=>'用户不存在'],201);
		}

		if($session_password != $user_result[0]->password){
			return Response::json(['error'=>true,'message'=>'密码验证失败'],201);
		}else{
			$role_pri = [];
			$role_id = explode(',', $user_result[0]->role_id);
			$roles = DB::select("select * from sys_role");
			foreach ($roles as $key => $value) {
				if(in_array($value->id,$role_id)){
					$role_pri[] = $value->privilege;
				}
			}

			$reuslt = ["id"=>$user_result[0]->id,"nativeid"=>$user_result[0]->nativeid,"name"=>$user_result[0]->name,"role"=>"教师","role_pri"=>implode(',', $role_pri)];
			return Response::json(['error'=>false,'result'=>$reuslt],200);
		}
	}
	
	/**
	 * 管理用户登录的统一函数
	 * 
	 * @param 		nativeid,password
	 * @return 		json
	 */
	public function login(){
		if(!Input::has('nativeid')||!Input::has('password')){
			return Response::json(['error'=>true,'message'=>'请输入用户名或密码'],201);
		}	
		$nativeid = Input::get('nativeid');
		$user_result = DB::select("select id,password,salt from teacher where nativeid = '".$nativeid."'");
		if(!$user_result){
			return Response::json(['error'=>true,'message'=>'用户不存在'],201);
		}
		$password = Input::get('password');
		$salt = $user_result[0]->salt;
		$salt_pass = md5($salt.$password);
		if($salt_pass == $user_result[0]->password){
			Session::put('nativeid',$nativeid);
			Session::put('password',$salt_pass);
			return Response::json(['error'=>false,'message'=>'登录成功'],200);
		}
		return Response::json(['error'=>true,'message'=>'密码错误'],201);
    }

    /**
	 * 管理用户注销登录的统一接口
	 * 
	 * @param 		nativeid  password
	 * @return 		json
	 */
    public function logout(){
    	$session_user = Session::get('nativeid');
		$session_password = Session::get('password');
		if($session_user ==''||$session_password ==''){
			return Response::json(['error'=>true,'message'=>'缺少重要参数'],201);
		}
		Session::forget('nativeid');
		Session::forget('password');
		return Response::json(['error'=>false,'result'=>true],200);
    }

    //修改密码
	public function setpwd($id){
		if(!Input::has('pass_old') || !Input::has('pass_new') || !Input::has('pass_new2')){
			return Response::json(['error'=>true,'message'=>'缺少重要参数'],200);
		}
		$pass_old = Input::get('pass_old');
		$pass_new = Input::get('pass_new');
		$pass_new2 = Input::get('pass_new2');
		$pass_arr = DB::select("select password,salt from teacher where id='".$id."'");
		$temp_salt = $pass_arr[0]->salt;
		$temp_pass = $pass_arr[0]->password;
		if($pass_new != $pass_new2)
			return Response::json(['error'=>true,'message'=>'两次密码不一致！'],200);
		
	    if($temp_pass == md5($temp_salt.Input::get('pass_old'))){
	    	$temp_pass = md5($temp_salt.Input::get("pass_new"));
	    	DB::table('ed_teacher')->where('id','=',$id)->update(array('password'=>$temp_pass));
	    	return Response::json(['error'=>false,'message'=>'修改密码成功'],201);
	     }else{
	    	return Response::json(['error'=>true,'message'=>"原密码错误！"], 200);	    	
	      }
	}
	/**
	 * 管理用户登录后显示的权限
	 * 
	 * @param 		
	 * @return 		json
	 */
	public function power(){
		$session_nativeid = Session::get('nativeid');

		//根据session_nativeid查询角色所对应的role_id
		$role_id = DB::select("select id,teacher_role_id from teacher_classes where nativeid = '".$session_nativeid."' ");
		$role_id_arr = explode(",",$role_id[0]->teacher_role_id);
		
		//根据role_id查询角色所对应的权限
        $power = DB::table('teacher_role')->select('name')->whereIn('id',$role_id_arr)->get();
        return Response::json(['error'=>false,'result'=>$power],200);
        
        
	}

}
